package com.hosier.service;

import com.aspose.slides.License;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;

import java.io.InputStream;
import java.io.OutputStream;

/**
 * @author hosier
 * @date 2021/07/08 文档转换服务
 */
public class OfficeConvertService {

    /**
     * 文档转换入口
     * @return
     */
    public void convert(InputStream source, OutputStream target) throws Exception {
        // 验证Licence
        if (!getLicense()) {
            return;
        }
        // 文档转换
        convert2PDF(source, target);
    }

    /**
     * 获取license
     * @return
     */
    public static boolean getLicense() {
        boolean result = false;
        try {
            InputStream is = OfficeConvertService
                            .class
                            .getClassLoader()
                            .getResourceAsStream("\\licence\\aspose-licence.xml");
            License aposeLic = new License();
            aposeLic.setLicense(is);
            is.close();
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 转换为PDF
     * @return
     */
    public void convert2PDF(InputStream source, OutputStream target) {
        // 生成presentation对象
        Presentation pres = new Presentation(source);
        // 保存为pdf
        pres.save(target, SaveFormat.Pdf);
    }
}
